﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hotel.Domain;
using Hotel.Util;

namespace HOTEL.Dao
{
    public class TaiKhoanDao:BaseDAO
    {
        public TAI_KHOAN Get(int id)
        {
            return _db.TAI_KHOANs.Where(p => p.MaNV.Equals(id)).SingleOrDefault();
        }
        public TAI_KHOAN CheckLogin(TAI_KHOAN obj)
        {
            return _db.TAI_KHOANs.Where(p => p.TenTaiKhoan.Equals(obj.TenTaiKhoan) 
                && p.MatKhau.Equals(obj.MatKhau)
                && p.Active).SingleOrDefault();
        }
        public bool CheckAvailable(TAI_KHOAN obj)
        {
            var tk =_db.TAI_KHOANs.Where(p => p.MaNV.Equals(obj.MaNV)).SingleOrDefault();
            if (tk != null)
                return true;
            else
                return false;
        }
        public Message Create(TAI_KHOAN obj)
        {
            Message mgs;
            try
            {
                _db.Connection.Open();
                if (CheckAvailable(obj) != true)
                {
                    _db.TAI_KHOANs.InsertOnSubmit(obj);
                    _db.SubmitChanges();
                    mgs = new Message(MessageConstants.I_CREATE_SUCCEED, MessageType.Info, "tài khoản");
                }
                else
                {
                    mgs = new Message(MessageConstants.E_FAILED_DUPLICATED_KEY, MessageType.Error, "tài khoản");
                }
            }
            catch(Exception ex)
            {
                mgs = ExceptionHandler.HandleInsertException(ex, "tài khoản ", obj.TenTaiKhoan);
            }
            return mgs;
        }
    }
}
